import java.io.*;

public class CopyVideoTest {
    public static void main(String[] args) {
        String source = "C:\\Users\\liyan\\Desktop\\JavaApi\\FileIo\\mountains.mp4";
        String target = "C:\\Users\\liyan\\Desktop\\JavaApi\\FileIo\\mountains_copy.mp4";
        long startTime = System.currentTimeMillis();
//        test1(source, target);  //254ms
        test2(source, target);   //53ms
        long endTime = System.currentTimeMillis();
        System.out.println("耗时：" + (endTime - startTime) + "ms");
    }

    public static void test1(String source, String target) {
        File file1 = new File(source);
        File file2 = new File(target);

        try (FileInputStream fis = new FileInputStream(file1);
             FileOutputStream fos = new FileOutputStream(file2)) {
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            System.out.println("复制成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test2(String source, String target) {
        File file1 = new File(source);
        File file2 = new File(target);
        try (
                FileInputStream fis = new FileInputStream(file1);
                FileOutputStream fos = new FileOutputStream(file2);
                BufferedInputStream bis = new BufferedInputStream(fis);
                BufferedOutputStream bos = new BufferedOutputStream(fos);
        ) {
            int len;
            byte[] bytes = new byte[1024];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            System.out.println("复制成功");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
